package com.nosweattechnet.placemap;

import java.util.ArrayList;
import java.util.concurrent.ExecutionException;

import android.app.ListActivity;
import android.app.SearchManager;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import android.widget.Toast;

import com.nosweattechnet.datatypes.Item;
import com.nosweattechnet.datatypes.ItemListAdapter;
import com.nosweattechnet.mapmyway.R;
import com.nosweattechnet.server.ItemSearchAsyncTask;

//Searches for Items given a given keyword and displays the results
public class ItemSearch extends ListActivity {
	private ArrayList<Item> itemObjects;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //gets the keyword and then performs the search
        Intent intent = getIntent();
        if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
            String query = intent.getStringExtra(SearchManager.QUERY);
            doItemSearch(query,DisplayItems.getPlaceID());
        }
        
        setContentView(R.layout.activity_search_items);
    }
    
    //Searches the database for Items containing the keyword supplied
    public void doItemSearch(String query,String placeid)
    {
    	//Gets items from the database containing the given keyword
    	ItemSearchAsyncTask isat = new ItemSearchAsyncTask(query,placeid);
    	isat.execute();
    	itemObjects = new ArrayList<Item>();
    	try {
    		itemObjects = isat.getItems();
    	}
    	catch (InterruptedException e) {}
    	catch (ExecutionException e) {}
    	
    	//If nothing was found then the user is told and the placemap is displayed
    	if(itemObjects.size()<=0){
    		Toast.makeText(this,R.string.no_search_results,Toast.LENGTH_LONG).show();
    		finish();
    	}
    	
    	//Removes any duplicates
    	for (int x = 0; x < itemObjects.size(); x++)
    	{
    		for (int y = 0; y < itemObjects.size(); y++)
    		{
    			String item1 = itemObjects.get(x).getId();
    			String item2 = itemObjects.get(y).getId();
    			if (item1.equals(item2)  && x != y)
    			{
    				itemObjects.remove(y);
    			}
    		}
    	}
    	
    	//Adds the items to the list
    	ItemListAdapter itemAdapter = new ItemListAdapter(ItemSearch.this, R.layout.list_item, itemObjects);
    	setListAdapter(itemAdapter);
    }
    
    @Override
    protected void onListItemClick(ListView l, View v, int position, long id){
    	Item selected =  itemObjects.get(position);
        DisplayItems.clear();
        DisplayItems.addItem(selected);
        finish();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_search_items, menu);
        return true;
    }

    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        	//When the user selects the "Show All" button all of the items from the search are displayed
        	case R.id.item_search_show_all:
        		DisplayItems.clear();
        		DisplayItems.addItems(itemObjects);
        		finish();
        		return true;
            case android.R.id.home:
                NavUtils.navigateUpFromSameTask(this);
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

}
